package com.atguigu.tingshu.order.handle;

import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;

@Component
@Slf4j
public class RedisDelayHandle {



    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderInfoService orderInfoService;
    /**
     * 监听延迟消息
     */
    @PostConstruct
    public void cancelOrder(){

        //1.创建一个线程
        new Thread(()->{
            //2.循环等待消息
            while(true){
                try {
                    //3.获取延迟对象，尝试获取消息
                    RBlockingDeque<String> blockingDeque = redissonClient.getBlockingDeque(KafkaConstant.QUEUE_ORDER_CANCEL);
                    //获取消息
                    String orderId = blockingDeque.take();
                    //判断
                    if(StringUtils.isNotEmpty(orderId)){


                        //关闭订单
                        orderInfoService.orderCancal(orderId);

                    }




                } catch (InterruptedException e) {
                   e.printStackTrace();
                }


            }




        }).start();





    }

}
